package ug.co.icom.kyazze.healthticket;

import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

import android.os.AsyncTask;

public class PutMongoHqTask extends AsyncTask<String, Void, String> {

		@SuppressWarnings("unused")
		private Exception exception;

		protected String doInBackground(String... urls) {
			try {
				URL url = new URL(
						"https://api.mongohq.com/databases/mHealth/collections/medical_records/documents/5298bae2fd9358dfc60001b3?_apikey=YPXfSPrBkC5EMptf7m6pxSlB3B2AcRUc6Mk7mWuPO0");
				HttpURLConnection connection = (HttpURLConnection) url
						.openConnection();
				connection.setRequestMethod("PUT");
				connection.setDoOutput(true);
				connection.setRequestProperty("Content-Type",
						"application/json");
				connection.setRequestProperty("Accept", "application/json");
				String a_name = "checking";
				String a_date = "checking";
				String a_severity = "checking";
				String a_reaction = "checking";
				String a_occurence =  "checking";
				String a_comments = "checking";

				OutputStreamWriter osw = new OutputStreamWriter(
						connection.getOutputStream());
				
				String query = String
						.format("{\"document\" : { \"$addToSet\" : {\"allergies\" : {\"name\" : \"%s\" , \"date_of_diagnosis\" : \"%s\", \"severity\" : \"%s\", \"reaction\" : \"%s\", \"occurence\" : \"%s\", \"additional_comments\" : \"%s\" }} }}",
								a_name, a_date, a_severity, a_reaction,
								a_occurence, a_comments);
				osw.write(query);
				osw.flush();
				osw.close();

				// System.err.println(connection.getResponseCode());
			} catch (Exception e) {
				this.exception = e;
				return null;
			}
			return null;
		}

	}


